/**  
* @Title: ServiceRecordController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年6月3日  
*/
package com.fortune.rainshine.system.controller;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.PageResult;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.service.service.OrderService;
import com.fortune.rainshine.service.service.ProductSaleService;
import com.fortune.rainshine.service.service.StockService;
import com.fortune.rainshine.system.service.CustomerCardService;
import com.fortune.rainshine.system.service.CustomerPayCardService;
import com.fortune.rainshine.system.service.CustomerService;
import com.fortune.rainshine.system.service.ServiceRecordService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.service.StockModel;
import com.fortune.rainshinecommon.model.system.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

/**  
* @ClassName: ServiceRecordController  
* @Description: TODO(各项数据统计记录展示)  
* @author 张鑫
* @phone 17561783855
* @date 2019年6月3日  
*    
*/
@Controller
@RequestMapping("/system/service")
public class ServiceRecordController extends BaseController{
    @Autowired
    private ServiceRecordService serviceRecordService;
    @Autowired
    private StockService stockService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerCardService customerCardService;
    @Autowired
    private CustomerPayCardService customerPayCardService;
    @Autowired
    private ProductSaleService productSaleService;
    
    private static final Logger logger = LoggerFactory.getLogger(ServiceRecordController.class);

    @RequiresPermissions("service:view")
    @RequestMapping()
    public String service(Model model) {
		// 获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		// 获取登录用户的按钮展示情况
		model.addAttribute("stockRecord", authorities.contains("service:stockRecord"));
		model.addAttribute("orderBuy", authorities.contains("service:orderBuy"));
		model.addAttribute("orderSale", authorities.contains("service:orderBuy"));
		model.addAttribute("customer", authorities.contains("service:customer"));
		model.addAttribute("customerCard", authorities.contains("service:customerCard"));
		model.addAttribute("payCard", authorities.contains("service:payCard"));
		model.addAttribute("productSale", authorities.contains("service:productSale"));
		model.addAttribute("productUse", authorities.contains("service:productSale"));
        
        return "service/service.html";
    }
    
    @RequiresPermissions("service:view")
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<ShopModel> list(Integer page, Integer limit, String searchKey, String searchValue,Integer shopId) {
        if (StringUtil.isBlank(searchValue)) {
            searchKey = null;
        }
        User user =this.getLoginUser();
        user.setShopId(shopId);
        return serviceRecordService.list(searchKey, searchValue,user);
    }
    
    @RequiresPermissions("service:stockRecord")
    @RequestMapping("/stockRecord")
    public String stock(Model model,String shopId) {
        //根据店铺id获取该店库存详情
        Object stocks = stockService.getStocksByShopId(shopId).getData();
        List<StockModel> list = (List<StockModel>)stocks;
        if(!list.isEmpty()) {
            model.addAttribute("empty", true);
        }else {
            model.addAttribute("empty", false);
        }
        
        model.addAttribute("stocks", stocks);
        return "service/stockRecord.html";
    }
    
    /**
     * @Title: recordDetail
     * @Description: 打开库存
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年6月12日
     */
    @RequiresPermissions("service:stockRecordDetail")
    @RequestMapping("/recordDetail")
    public String recordDetail(Model model,String id) {
        return "service/stockRecordDetail.html";
    }
    
    /**
     * @Title: stockRecordDetail
     * @Description: 查看库存明细
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年6月12日
     */
    @RequiresPermissions("service:stockRecordDetail")
    @ResponseBody
    @GetMapping("/stockRecordDetail")
    
    public MsgModel stockRecordDetail(Integer page, Integer limit, String searchKey, String searchValue, Integer type) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue);
            parm.setType(type);
            return stockService.stockRecordDetail(parm);
        } catch (Exception e) {
            logger.error("stockRecordDetail ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
    
    /**
     * @Title: stockOrderBuy
     * @Description: 查看采购
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年6月12日
     */
    @RequiresPermissions("service:orderBuy")
    @ResponseBody
    @GetMapping("/orderBuy")
    public MsgModel orderBuy(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId,Integer type) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue,shopId);
            parm.setType(type);
            return orderService.list(parm);
        } catch (Exception e) {
            logger.error("orderBuy ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
    
    /**
     * @Title: customer
     * @Description: 顾客管理
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年6月12日
     */
    @RequiresPermissions("service:customer")
    @ResponseBody
    @GetMapping("/customer")
    public MsgModel customer(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue,shopId);
            return customerService.list(parm);
        } catch (Exception e) {
            logger.error("customer ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
    
    /**
     * @Title: customerCard
     * @Description: 会员卡购买记录
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年6月12日
     */
    @RequiresPermissions("service:customerCard")
    @ResponseBody
    @GetMapping(value = "/customerCard")
    public MsgModel customerCard(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue,shopId);
            return customerCardService.list(parm);
        } catch (Exception e) {
            logger.error("customerCard ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
    
    /**
     * @Title: payCard
     * @Description: 会员服务
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年6月12日
     */
    @RequiresPermissions("service:payCard")
    @ResponseBody
    @GetMapping("/payCard")
    public MsgModel payCard(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue,shopId);
            return customerPayCardService.list(parm);
        } catch (Exception e) {
            logger.error("payCard ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
    
    @RequiresPermissions("service:productSale")
    @ResponseBody
    @GetMapping("/productSale")
    public MsgModel productSale(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId, Integer type) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue,shopId);
            parm.setType(type);
            return productSaleService.list(parm);
        } catch (Exception e) {
            logger.error("productSale ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
        
    }
    
    @RequiresPermissions("service:productSale")
    @ResponseBody
    @GetMapping("/productSaleDetail")
    public MsgModel productSaleDetail(Integer page, Integer limit, String searchKey, String searchValue, String code, Integer type) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue);
            parm.setCode(code);
            parm.setType(type);
            return productSaleService.orderDetail(parm);
        } catch (Exception e) {
            logger.error("productSale ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
        
    }
}
